#include <iostream>
#include <string.h>
#include <stdio.h>

using namespace std;

/// Nhập vào một chuỗi, hãy loại bỏ những khoảng trắng thừa trong chuỗi
void chuanHoaChuoi(char *str) {

    char s[100];
    //char *s;//-->se co loi;
    strcpy(s,str);//can copy str sang s vi str la chuoi hang, khong the thay doi

    while(s[0]==' ')
        strcpy(&s[0],&s[1]);

    int len =  strlen(s);
    while(s[len-1]==' ') {
        strcpy(&s[len-1],&s[len]);
        len=strlen(s);
    }

    for(int i=0; i<len; i++)
        if(s[i]==' ' && s[i+1]==' ') {
            strcpy(&s[i],&s[i+1]);
            i--;
        }

    printf("\n%s_",s);

}

/// Ham chuan hoa chuoi, dung cac ham cua C++
void chuanHoaChuoi_CplusPlus(string str) {
    while(str[0]==' ')
        str.erase(0,1);

    int len = str.length();
    while(str[len-1]==' ') {
        str.erase(len-1,1);
        len=str.length();
    }

    for(int i=0; i<len; i++)
        if(str[i]==' '&& str[i+1]==' ') {
            str.erase(i,1);
            i--;
        }
    cout<<endl<<str<<"_";//them dau _ de kiem tra ket thuc chuoi nam o dau
}



int main() {
    cout<<endl<<"Chuan hoa chuoi s1, dung C\n ";
    char s1[100];
    gets(s1);
    chuanHoaChuoi(s1);

    cout<<endl<<"Chuan hoa chuoi s2, dung C++\n";
    string s2;
    getline(cin,s2);
    chuanHoaChuoi_CplusPlus(s2);

    cout<<endl;
    return 0;
}
